<?php
// +----------------------------------------------------------------------
// | YikeCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://zhuizhan.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: Doo <yws_sky@163.com> | <Qq:1151414552>
// +----------------------------------------------------------------------
namespace Member\Controller;
use Org\Yike\Category;
use Think\Controller;
class CommonController extends Controller
{
    public function _initialize()
    {

        $this->cotroller = strtolower(CONTROLLER_NAME);
        //网站配置
        $where = "name='site' or name='contact' or name='nav' or name='logo'";
        $setting = $setting = M('setting')->where($where)->select();
        $arr = array();
        foreach ($setting as $s) {
            $arr[$s['name']] = json_decode($s['data'], true);
        }
        $this->siteName = $arr['site']['title'];
        $this->site = $arr['site'];
        $this->contact = $arr['contact'];
        $this->logo = $arr['logo'];
        $this->site_nav = $arr['nav'];

        //留言统计
        $where = array('status' => array('neq', 1), 'store_id' => $_SESSION['userInfo']['id']);
        $this->rental_count = M('order_rental')->where($where)->count();
        $this->used_count = M('order_used')->where($where)->count();
        //左边菜单
        $user = getLoginUser();
        if ($user) {
            $cateid = M('user_info')->where('user_id=' . session('userInfo.id'))->getField('cate_id');
            $authIds = trim(M('user_cate')->where('id=' . $cateid)->getField('auth'), ',');
            $map = array(
                'mid' => array('in', __MENUS__ . $authIds),
                'status' => 1
            );
            $member_menu = M('member_menu')->where($map)->order('pmid asc')->select();
            foreach ($member_menu as $vo) {
                if ($vo['pmid'] == 0) {
                    $menus[$vo['mid']]['title'] = $vo['title'];
                } else {
                    $menus[$vo['pmid']]['sub'][] = array(
                        'id' => $vo['mid'],
                        'title' => $vo['title'],
                        'url' => U($vo['url'])
                    );
                }

            }

            $this->assign ('user', $user);
            $this->assign('menus', $menus);
        }

    }


    public function myError()
    {
        send_http_status(404);
        $this->display('Public:404');
    }

    public function is_login()
    {
        $user_id = $_SESSION['userInfo']['id'];
        if (!$user_id) {
            $this->redirect('Login/index');
        } else {
            $user = M('user')->where(array('id' => $user_id))->find();
            $map['user_id'] = $user['id'];
            $user['userInfo'] = D("UserInfo")->where($map)->find();
            $user['account'] = D("UserAccount")->where($map)->find();
            $user['userCate'] = M('UserCate')->where(array('id' => $user['userInfo']['cate_id']))->field('id,name')->find();
            $this->user = $user;
            $this->userCate = $user['userCate'];
            return $user;
        }
    }

    public function seoKey($seo, $title = 'title', $end = '', $site = 1)
    {
        $seo_title = $seo['seo_title'] ? $seo['seo_title'] : $seo[$title];
        $seo_keys = $seo['seo_keys'] ? $seo['seo_keys'] : $seo_title;
        if ($end) {
            $seo_title = $seo_title . $end;
            $seo_keys = $seo_keys . $end;
        }
        if ($site) {
            $siteName = '_' . $this->siteName;
        }
        $this->seo = array(
            'title' => $seo_title . $siteName,
            'keys' => $seo_keys,
            'desc' => $seo['seo_desc']
        );
    }

    protected function _upload($dir = '')
    {
        if (!$dir) $dir = (CONTROLLER_NAME == 'Ad' or CONTROLLER_NAME == 'Advert') ? 'Images' : CONTROLLER_NAME;
        $savepath = $dir . '/';
        $upload = new \Think\Upload();
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = $savepath; // 设置附件上传（子）目录
        $upload->saveName = array('uniqid', date("His"));
        $upload->subName = array('date', 'Ymd');
        if (!file_exists($upload->rootPath . $savepath)) {
            mkdir($upload->rootPath . $savepath);
        }
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功 获取上传文件信息
            return $info;
        }
    }

    //增加时图片上传
    public function _insertPic($data, $info_pic = 'info_pic')
    {
        //上传图片
        if (!empty($_FILES['img']['name'])) {
            $img = $this->_upload();
            $data['img'] = $img['img']['savepath'] . $img['img']['savename'];
        } elseif ($info_pic == 1) {
            $content = stripslashes($data['info']);
            if (preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)) {
                $data['img'] = $matches[3][0];
            }
        }

        //多图上传
        if (isset($_POST['pic']) && !empty($_POST['pic'])) {
            $pic = array();
            $images = $_POST['pic'];
            foreach ($images['pic'] as $key => $v) {
                $pic[] = array('img' => $v, 'alt' => $images['alt'][$key], 'sort' => $images['sort'][$key]);
            }
            $data['file'] = !empty($pic) ? json_encode($pic) : '';
        }
        return $data;
    }
    //多表单图片上传
    public function upload_($type){
        $imgType = array ('image/pjpeg','image/jpeg','image/gif','image/png');
        $savePath = 'Uploads/unit/'.date ('Y-m-d').'/';
        $msg = '';
        $data = array ();
        if(is_uploaded_file($_FILES[$type]['tmp_name'][0])){
            $upfile = $_FILES[$type];
            $count = count ($upfile['name']);
            for ($i=0; $i<$count; $i++){
                $file = $upfile['tmp_name'][$i];
                $exts = pathinfo ($upfile['name'][$i], PATHINFO_EXTENSION);
                if (in_array ($upfile['type'][$i], $imgType )){
                    if ($upfile['error'][$i] == 0){
                        $nwefile = $savePath.time().'_'.mt_rand().'.'.$exts;
                        if(!is_dir($savePath)) {
                            mkdir($savePath, 0777, true);
                        }
                        if (move_uploaded_file($file, $nwefile)){
                            $data[$i]['data'] = empty ($nwefile)? '': $nwefile;
                            $msg = '上传成功';
                        }else{
                            $msg = "上传失败";
                        }
                    }else{
                        switch ($upfile['error'][$i]){
                            case '1':
                                $msg = '超过了文件大小';
                                break;
                            case '2':
                                $msg = "超过了文件的大小MAX_FILE_SIZE选项指定的值";
                                break;
                            case '3':
                                $msg = '文件只有部分被上传';
                                break;
                            case '4':
                                $msg = "没有文件被上传";
                                break;
                        }
                    }
                }else{
                    $msg = '文件类型不合法或者为空';
                }
                $data[$i]['msg'] = $msg;
            }
        }
        return $data;
    }


}